<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-container">
          <div class="logo">管理系统</div>
          <div class="user-info">
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                {{ authStore.user?.username || '用户' }}
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      
      <el-container>
        <el-aside width="200px">
          <el-menu
            default-active="1"
            class="el-menu-vertical"
            router
          >
            <el-menu-item index="/">
              <el-icon><HomeFilled /></el-icon>
              <span>首页</span>
            </el-menu-item>
            <el-menu-item index="/profile">
              <el-icon><User /></el-icon>
              <span>个人信息</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        
        <el-main>
          <el-card>
            <template #header>
              <div class="card-header">
                <h2>欢迎使用管理系统</h2>
              </div>
            </template>
            <div class="welcome-content">
              <p>您已成功登录系统</p>
              <p v-if="authStore.user">
                <strong>用户名：</strong> {{ authStore.user.username }}<br>
                <strong>用户角色：</strong> {{ roleText }}<br>
                <strong>用户ID：</strong> {{ authStore.user.id }}
              </p>
            </div>
          </el-card>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted } from 'vue'
import { useAuthStore } from '@/store/auth'
import { useRouter } from 'vue-router'
import { ArrowDown, HomeFilled, User } from '@element-plus/icons-vue'

const authStore = useAuthStore()
const router = useRouter()

// 角色文本
const roleText = computed(() => {
  const role = authStore.userRole
  switch (role) {
    case 0:
      return '普通用户'
    case 1:
      return '管理员'
    case 2:
      return '超级管理员'
    default:
      return '未知角色'
  }
})

// 下拉菜单命令处理
const handleCommand = (command: string) => {
  if (command === 'logout') {
    authStore.logout()
  } else if (command === 'profile') {
    router.push('/profile')
  }
}

// 组件挂载时检查认证状态
onMounted(() => {
  authStore.checkAuth()
})
</script>

<style scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #409EFF;
  color: white;
  line-height: 60px;
  padding: 0 20px;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 18px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.el-aside {
  background-color: #f5f7fa;
  color: #333;
  border-right: 1px solid #e6e6e6;
}

.el-menu {
  border-right: none;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-content {
  line-height: 1.8;
}
</style> 